package com.css.fxfzysx.modules.cztDataMange.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.cztDataMange.entity.CbvInfoEntity;
import com.css.fxfzysx.modules.cztDataMange.entity.CpvInfoEntity;
import com.css.fxfzysx.modules.cztDataMange.param.BuildingVulnerabilityParam;
import com.css.fxfzysx.modules.cztDataMange.param.ConfirmMoveIdParam;
import com.css.fxfzysx.modules.cztDataMange.service.BacInformationService;
import com.css.fxfzysx.modules.cztDataMange.vo.BacInformationVo;
import com.css.fxfzysx.modules.cztDataMange.vo.CbvInfoVo;
import com.css.fxfzysx.modules.cztDataMange.vo.QueryBacInformationVo;
import com.css.fxfzysx.modules.cztDataMange.vo.QueryBasicVo;
import com.css.fxfzysx.modules.vabStructure.param.CustomVulnerabilityParam;
import com.css.fxfzysx.util.PlatformObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 建筑物区域特征信息导入
 */
@RestController
@RequestMapping("bacInformation")
@Slf4j
public class BacInformationController {

    @Autowired
    private BacInformationService bacInformationService;

    /**
     * 导入建筑物易损性信息   批量导入
     */
    @PostMapping("uploadBvInformation")
    public RestResponse uploadBvInformation(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return bacInformationService.uploadBvInformation(file);
    }

    /**
     * 导入建筑物易损性信息   单个导入
     */
    @PostMapping("uploadSingleBvInformation")
    public RestResponse uploadSingleBvInformation(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return bacInformationService.uploadSingleBvInformation(file);
    }

    /**
     * 建筑物易损性矩阵信息   添加
     */
    @PostMapping("saveBvInformation")
    public RestResponse saveBvInformation(@RequestBody CbvInfoVo cbvInfo) {
        return bacInformationService.saveBvInformation(cbvInfo);
    }

    /**
     * 建筑物曲线   单个导入
     */
    @PostMapping("uploadSingleBvInformationCurve")
    public RestResponse uploadSingleBvInformationCurve(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return bacInformationService.uploadSingleBvInformationCurve(file);
    }

    /**
     * 建筑物曲线   批量导入
     */
    @PostMapping("uploadBvInformationCurve")
    public RestResponse uploadBvInformationCurve(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return bacInformationService.uploadBvInformationCurve(file);
    }

    /**
     * 建筑物易损性曲线信息   添加
     */
    @PostMapping("saveBvInformationCurve")
    public RestResponse saveBvInformationCurve(@RequestBody CbvInfoVo cbvInfo) {
        return bacInformationService.saveBvInformationCurve(cbvInfo);
    }


    /**
     * 导入砖混结构参数信息
     */
    @PostMapping("uploadPobStructure")
    public RestResponse uploadPobStructure(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return bacInformationService.uploadPobStructure(file);
    }

    /**
     * 导入钢筋混凝土参数信息
     */
    @PostMapping("uploadRcpInformation")
    public RestResponse uploadRcpInformation(@RequestParam MultipartFile file) {
        RestResponse restResponse = null ;
        try {
            if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
                restResponse =  RestResponse.fail("文件格式不支持!");
            }
            restResponse = bacInformationService.uploadRcpInformationNew(file);
        }catch (Exception e){
            restResponse  = RestResponse.fail(PlatformObjectUtils.isNotEmpty(e.getMessage())?e.getMessage():"导入失败!");
        }

        return restResponse ;
    }

    /**
     * 导入高层建筑参数信息
     */
    @PostMapping("uploadHbpInformation")
    public RestResponse uploadHbpInformation(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return bacInformationService.uploadHbpInformation(file);
    }

    /**
     * 导入概率地震易损性分析数据
     */
    @PostMapping("uploadPsaData")
    public RestResponse uploadPsaData(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return bacInformationService.uploadPsaData(file);
    }


    /**
     * 修改概率地震易损性信息
     */
    @PostMapping("/updateCpvInfoEntity")
    public RestResponse updateCpvInfoEntity(@RequestBody CpvInfoEntity entity) {
        return bacInformationService.updateCpvInfoEntity(entity);
    }

    /**
     * 建筑物区域特征信息修改    TODO 这个功能前端未实现，如果要修改功能，代码需要改，钢筋混凝土表需要更改
     */
    @PostMapping("updateBacInformation")
    public RestResponse updateBacInformation(@RequestBody BacInformationVo bacInformationVo) {
        return bacInformationService.updateBacInformation(bacInformationVo);
    }

    /**
     * 建筑物区域特征信息删除
     */
    @GetMapping("deleteBacInformation")
    public RestResponse deleteBacInformation(String ids, String type,String province) {
        return bacInformationService.deleteBacInformation(ids, type,province);
    }

//    /**
//     * 建筑物区域特征信息修改回显   TODO 这个功能前端未实现，如果要回显功能，代码需要改，钢筋混凝土表需要更改
//     */
//    @GetMapping("queryBacInformationById")
//    public RestResponse queryBacInformationById(String id, String type) {
//        return bacInformationService.queryBacInformationById(id, type);
//    }

    /**
     * 建筑物区域特征信息分页查询
     */
    @GetMapping("queryBacInformation")
    public RestResponse queryBacInformation(QueryBacInformationVo queryBacInformationVo, HttpServletRequest request) {
        return bacInformationService.queryBacInformation(queryBacInformationVo, request);
    }

    /**
     * 移入，移出基础库
     */
    @PostMapping("moveBasicLibrary")
    public RestResponse moveBasicLibrary(@RequestBody List<CustomVulnerabilityParam> paramList) {
        return bacInformationService.moveBasicLibrary(paramList);
    }

    /**
     * 确定移入基础库
     * @param
     * @return
     */
    @PostMapping("confirmMoveIn")
    public RestResponse confirmMoveIn(@RequestBody ConfirmMoveIdParam param) {
        try {
            boolean result = bacInformationService.confirmMoveIn(param);
            if(result){
                return RestResponse.succeed("移入成功!");
            }else {
                return RestResponse.fail("失败,所选数据存在相同条件!");
            }
        } catch (Exception e) {
            log.error("post confirmMoveIn error:{}",e.getMessage(),e);
            return RestResponse.fail("移入失败!");
        }
    }

    /**
     * 建筑物区域特征信息详情
     */
    @GetMapping("getBacInfoById")
    public RestResponse getBacInfoByNumber(String id,String province) {
        return bacInformationService.getBacInfoByNumber(id,province);
    }

    /**
     * 根据id和类型导出
     */
    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response, String id, String type) {
        bacInformationService.exportExcel(response,id,type);
    }

    /**
     * 建筑物区域特征信息修改
     */
    @PostMapping("updateBuildingArea")
    public RestResponse updateBuildingArea(@RequestBody BuildingVulnerabilityParam param) {
        return bacInformationService.updateBuildingArea(param);
    }
}
